﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using Telerik.Web.UI;
using System.Data.SqlClient;
using Reconciliation.Services;
using System.Text;
using System.Collections;

namespace Reconciliation.Web
{
    public partial class Reconciliation_Invoice : System.Web.UI.Page
    {
        public int InvoiceID
        {
            get
            {
                if (ViewState["InvoiceID"] != null)
                    return (int)ViewState["InvoiceID"];
                else
                    return 0;
            }
            set
            {
                ViewState["InvoiceID"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            HeadMenu.MenuID = 1;
            AddAjaxSettings();
            FilterControl.PageName = "Invoice";
            FilterControl.LoadVariables();

            BindInvoiceGridMonthView();
            
            // function for view2
            // GetOrderList(int invoiceID)            
        }

        private void AddAjaxSettings()
        {
            /** add controls top ajax panel **/         
            
            ImageButton imgButton = (ImageButton)FilterControl.FindControl("imgApply");
            //Add the necessary AJAX setting programmatically
            RadAjaxManager1.AjaxSettings.AddAjaxSetting(imgButton, RadGridOrderList, RadAjaxLoadingPanel1);

            DropDownList ddType = (DropDownList)FilterControl.FindControl("ddType");
            RadAjaxManager1.AjaxSettings.AddAjaxSetting(ddType, RadGridOrderList, RadAjaxLoadingPanel1);

            //TextBox txtDate = (TextBox)FilterControl.FindControl("txtDateFromTo");
            //RadAjaxManager1.AjaxSettings.AddAjaxSetting(txtDate, RadGridOrderList, RadAjaxLoadingPanel1);

            DropDownList ddBank_Invoice = (DropDownList)FilterControl.FindControl("ddBank_Invoice");
            RadAjaxManager1.AjaxSettings.AddAjaxSetting(ddBank_Invoice, RadGridOrderList, RadAjaxLoadingPanel1);

            //Panel mstPnl = new Panel();
            //mstPnl = (Panel)Master.FindControl("ltrIcons");
            //ImageButton expExl = new ImageButton();
            //expExl = (ImageButton)mstPnl.FindControl("btnExpXls");
            //RadAjaxManager1.AjaxSettings.AddAjaxSetting(expExl, mstPnl, RadAjaxLoadingPanel1);

            /** end ajsx panel **/

        }

       
        protected void OnAjaxUpdate(object sender, ToolTipUpdateEventArgs args)
        {
            this.UpdateToolTip(args.Value, args.UpdatePanel);
        }

        private void UpdateToolTip(string elementID, UpdatePanel panel)
        {
            Control ctrl = Page.LoadControl("~/Users Control/InvoiceAuditorComments.ascx");

            panel.ContentTemplateContainer.Controls.Add(ctrl);
            
            Reconciliation.Web.Users_Control.InvoiceAuditorComments comments = (Reconciliation.Web.Users_Control.InvoiceAuditorComments)ctrl;
            comments.InvoiceID = int.Parse(elementID);  
        }
        
        public void BindInvoiceGridMonthView()
        {
            RadGridOrderList.DataSource = Reconciliation.Services.Services.GetInvoiceListMonthWise(FilterControl.StartDate, FilterControl.EndDate, FilterControl.BankID, FilterControl.Type);
            RadGridOrderList.DataBind();
            BasePage.Grid = RadGridOrderList;
        }

        public void BindInvoiceGridDetailView(int InvoiceID)
        {
            RadGridDetailView.DataSource = Reconciliation.Services.Services.GetOrderList(InvoiceID);
            RadGridDetailView.DataBind();
        }

        protected void RadGridOrderList_PageSizeChanged(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            BindInvoiceGridMonthView();
        }

        protected void RadGridOrderList_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
                Control image = e.Item.FindControl("lnkComment");
                HiddenField hddInvoiceID = (HiddenField)e.Item.FindControl("hiddInvoiceID");

                this.RadToolTipManager1.TargetControls.Add(image.ClientID, hddInvoiceID.Value.ToString(), true);
            }
        }

        protected void RadGridDetailView_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
                Control image = e.Item.FindControl("lnkAuditorComment");
                this.RadToolTipManager1.TargetControls.Add(image.ClientID, InvoiceID.ToString(), true);

                Control image2 = e.Item.FindControl("lnkClientComment");
                this.RadToolTipManager1.TargetControls.Add(image2.ClientID, InvoiceID.ToString(), true);

                HyperLink lnkDetails = (HyperLink)e.Item.FindControl("lnkViewDetails");
                lnkDetails.Attributes.Add("onclick", "viewCaseViewDetails('" + lnkDetails.Text + "');");
            }
        }
                
        protected void RadGridOrderList_SortCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            BindInvoiceGridMonthView();
        }

        protected void RadGridOrderList_PageIndexChanged(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            BindInvoiceGridMonthView();
        }

        protected void RadGridDetailView_SortCommand(object source, Telerik.Web.UI.GridSortCommandEventArgs e)
        {
            BindInvoiceGridDetailView(InvoiceID);

            //GridSortExpression sortExpr = new GridSortExpression();

            //sortExpr.FieldName = e.SortExpression;
            //sortExpr.SortOrder = GridSortOrder.Descending;

            //e.Item.OwnerTableView.SortExpressions.AddSortExpression(sortExpr);
            //RadGridDetailView.Rebind();            
            
        }

        protected void RadGridDetailView_PageIndexChanged(object source, Telerik.Web.UI.GridPageChangedEventArgs e)
        {
            RadGridDetailView.CurrentPageIndex = e.NewPageIndex;
            BindInvoiceGridDetailView(InvoiceID);
        }

        protected void RadGridOrderList_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "DetailView")
            {
                LinkButton lnkDate = new LinkButton();
                lnkDate=(LinkButton)e.Item.FindControl("lnkDetailView");
                lblMonth.Text = lnkDate.Text;
                GridDataItem item = (GridDataItem)e.Item;
                lblMSMShortage.Text = item["MsmShortage"].Text.ToString();
                lblClientShortage.Text = item["ClientShortage"].Text.ToString();

                Label lblInvDate = new Label();
                lblInvDate = (Label)e.Item.FindControl("lblInvDate");
                lblInvoiceDate.Text = lblInvDate.Text;
                lblAmountPaid.Text = item["AmountPaid"].Text.ToString();

                Label lblStatus = new Label();
                lblStatus = (Label)e.Item.FindControl("lblPaidStatus");
                lblAmountStatus.Text = lblStatus.Text;

                Label lblAmtDue = new Label();
                lblAmtDue = (Label)e.Item.FindControl("lblAmountDue");
                lblAmountDue.Text = lblAmtDue.Text;

                Label lblComment = new Label();
                lblComment = (Label)e.Item.FindControl("lblComment");
                lblClientComment.Text = lblComment.Text;

                HiddenField hddInvoiceID = (HiddenField)e.Item.FindControl("hiddInvoiceID");
                InvoiceID = int.Parse(hddInvoiceID.Value);

                multiView1.SetActiveView(View2);

                this.RadToolTipManager1.TargetControls.Add(lnkDetailViewClientComment.ClientID, InvoiceID.ToString(), true);
                // Bind Items
                BindInvoiceGridDetailView(InvoiceID);
                                
                //end bind items
                //DropDownList ddBank_Invoice = (DropDownList)FilterControl.FindControl("ddBank_Invoice");
                //ddBank_Invoice.Visible = false;
                //RadGridOrderList.Visible = false;
                pnl2.Visible = true;
                FilterControl.Visible = false;
                //pnl1.Visible = false;
            }           

            if (e.CommandName == "Edit")
            {
                ((LinkButton)(e.Item.FindControl("btnEditSelected"))).Visible = false;
                ((LinkButton)(e.Item.FindControl("btnUpdateEdited"))).Visible = false;
                ((LinkButton)(e.Item.FindControl("btnEditCancel"))).Visible = true;
                BindInvoiceGridMonthView();
            }
            if (e.CommandName == "Update")
            {

                TextBox txtAmountPaid = (TextBox)e.Item.FindControl("txtAmountPaid");
                BindInvoiceGridMonthView();
            }
            else if (e.CommandName == "Cancel")
            {
                BindInvoiceGridMonthView();
            }
        }


        public static string GetMonthName(DateTime date, bool abbrev)
        {
            if (abbrev) return date.ToString("MMM");
            return date.ToString("MMMM");
        }


        protected void LinqDataSourceInvoice_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            foreach (KeyValuePair<string, object> kvp in e.WhereParameters)
            {
                if (kvp.Value == null)
                {
                    e.Cancel = true;
                }

            }
        }

        protected void lnkDetailView_Click(object sender, CommandEventArgs e)
        {            
            multiView1.SetActiveView(View2);

            InvoiceID = int.Parse(e.CommandArgument.ToString());
                        
            // Bind Items
            BindInvoiceGridDetailView(InvoiceID);
             
            // end bind items
            //DropDownList ddBank_Invoice = (DropDownList)FilterControl.FindControl("ddBank_Invoice");
            //ddBank_Invoice.Visible = false;
            //RadGridOrderList.Visible = false;
            pnl2.Visible = true;
            FilterControl.Visible = false;            
            //pnl1.Visible = false;
            
        }
        protected void lnkMonthView_Click(object sender, EventArgs e)
        {
            InvoiceID = 0;
            multiView1.SetActiveView(View1);
            pnl2.Visible = false;
            FilterControl.Visible = true;    
        }          
}
}
